package com.hardroid.workload.ui.fragment;

import android.app.ListFragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hardroid.workload.R;
import com.hardroid.workload.utils.HardroidUtils;
import com.hardroid.workload.utils.ITallySelected;
import com.hardroid.workload.utils.Singleton;
import com.hardroid.workload.utils.TalliesAdapter;
import com.hardroid.workload.workcalc.Employer;
import com.hardroid.workload.workcalc.ObsvDay;
import com.hardroid.workload.workcalc.Tally;

public class SheetFragment extends ListFragment implements ITallySelected {

	private Employer employer;
	private TalliesAdapter adapter;
	private ObsvDay day;
	// private SheetStatusView statusView;

	private TextView pText;
	private TextView npText;
	private TextView uText;

	private TextView dateText;
	private TextView startText;
	private TextView endText;

	public int employerId = -999;
	public int dayId = -999;

	public SheetFragment() {
		// TODO Auto-generated constructor stub
		super();
		Log.i("Sheet Fragment", "Constructor");
	}

	public SheetFragment(int id, int did) {
		// TODO Auto-generated constructor stub
		super();
		this.employerId = id;
		this.dayId = did;
		Log.i("Sheet Fragment", "Constructor " + id + " , " +did);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Log.i("Sheet Fragment", "onCreate" + employerId);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		// if (getArguments()!=null){
		Log.i("Sheet Fragment", "onCreateView " + employerId);
		return inflater.inflate(R.layout.fragment_sheet, container, false);
		// }else{
		// return super.onCreateView(inflater, container, savedInstanceState);
		// }
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);

		LayoutInflater inflater = getActivity().getLayoutInflater();
		ViewGroup header = (LinearLayout) inflater.inflate(
				R.layout.list_header_tally, getListView(), false);
		getListView().addHeaderView(header);

		pText = (TextView) getView().findViewById(R.id.sheet_status_p);
		npText = (TextView) getView().findViewById(R.id.sheet_status_np);
		uText = (TextView) getView().findViewById(R.id.sheet_status_u);

		dateText = (TextView) getView().findViewById(R.id.day_status_date);
		startText = (TextView)getView(). findViewById(R.id.day_status_start_work);
		endText = (TextView)getView(). findViewById(R.id.day_status_end_work);				
		
		
		
		employer = Singleton.currentProject.lEmployer.get(employerId);
		day = employer.sheet.getObservation(dayId);
		adapter = new TalliesAdapter(getActivity(),
				employer.sheet.getObservation(dayId).lTally, this);

		setListAdapter(adapter);
				

		Log.i("Sheet Fragment", "onActivityCreated " + employerId);

		// if (id != 0 || Singleton.currentProject.lEmployer.size() != 0) {
		// Employer employer = Singleton.currentProject.lEmployer.get(id);
		// ObsvDay day = employer.sheet.getTodayObservation();
		// day.lTally.add(new Tally());
		// if (day != null) {
		// adapter = new TalliesAdapter(getActivity(), day.lTally);
		// setListAdapter(adapter);
		// }
		//
		// }
		updateView();
	}


	public static SheetFragment createInstance(int id, int did) {
		Log.i("Sheet Fragment", "createInstance " + id + "," +did);
		SheetFragment retval = new SheetFragment(id,did);
		Log.i("Sheet Fragment", "endCreateInstance");
		return retval;
	}

	@Override
	public void onTallySelected() {
		// TODO Auto-generated method stub
		updateView();
	}

	
	public void updateView() {
		int p = 0, np = 0, u = 0;
		for (Tally l : adapter.getTallies()) {
			if (l.productive) {
				p++;
			} else if (l.nProductive) {
				np++;
			} else if (l.unknown) {
				u++;
			}
		}

		pText.setText(p + "");
		npText.setText(np + "");
		uText.setText(u + "");
		Log.i("Sheet Fragment", "Update view " + pText.getText().toString()
				+ " , " + npText.getText().toString() + " , "
				+ uText.getText().toString());
		
		
		dateText.setText(HardroidUtils.dateToSDate(day.obsvDate));
		startText.setText(HardroidUtils.dateToTime(day.workStart));
		endText.setText(HardroidUtils.dateToTime(day.workEnd));
		
//		HardroidUtils.setListViewHeightBasedOnChildren(getListView());
	}
}
